﻿using System;
using System.Collections.Generic;
using System.Text;

namespace IOP.OpenType.Tables
{
    /// <summary>
    /// 字符映射表
    /// </summary>
    public class CmapTable : Table
    {
        /// <summary>
        /// 版本
        /// </summary>
        public ushort Version { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public ushort NumTables { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public EncodingRecord[] EncodingRecords { get; set; }
        /// <summary>
        /// 子表
        /// </summary>
        public CmapSubTable[] SubTables { get; set; }
        /// <summary>
        /// 获取字形Id
        /// </summary>
        /// <param name="platformID"></param>
        /// <param name="encodingID"></param>
        /// <param name="c"></param>
        /// <returns></returns>
        public uint GetGlyphId(ushort platformID, ushort encodingID, char c)
        {
            foreach (var item in SubTables)
            {
                if (item == null) continue;
                if (item.Encoding.PlatformID == platformID && item.Encoding.EncodingID == encodingID)
                {
                    return item.GetGlyphId(c);
                }
            }
            throw new NotSupportedException($"Cannot get glyphId with platformID : {platformID} and encodingID : {encodingID}");
        }

        /// <summary>
        /// 获取最大有字形字符
        /// </summary>
        /// <param name="platformID"></param>
        /// <param name="encodingID"></param>
        /// <returns></returns>
        public char GetMaxGlyphCharacter(ushort platformID, ushort encodingID)
        {
            foreach (var item in SubTables)
            {
                if (item == null) continue;
                if (item.Encoding.PlatformID == platformID && item.Encoding.EncodingID == encodingID)
                {
                    return item.GetMaxCharacter();
                }
            }
            throw new NotSupportedException($"Cannot get glyphId with platformID : {platformID} and encodingID : {encodingID}");
        }
        /// <summary>
        /// 获取最小有字形字符
        /// </summary>
        /// <param name="platformID"></param>
        /// <param name="encodingID"></param>
        /// <returns></returns>
        public char GetMinGlyphCharacter(ushort platformID, ushort encodingID)
        {
            foreach (var item in SubTables)
            {
                if (item == null) continue;
                if (item.Encoding.PlatformID == platformID && item.Encoding.EncodingID == encodingID)
                {
                    return item.GetMinCharacter();
                }
            }
            throw new NotSupportedException($"Cannot get glyphId with platformID : {platformID} and encodingID : {encodingID}");
        }
    }
}
